<template>
  <div v-loading="loading" class="b-cont">
    <div class="detail-box">
      <div class="detail-item">
        <label class="detail-label">
          {{ $t('crm.product.25cdd50e5ca0641f0ed60f92405ef9aa') }}
        </label>

        <div class="detail">
          <flexbox class="detail-images">
            <div
              v-for="(item, index) in primaryList"
              :key="index"
              class="show-img">
              <img
                :key="item.url"
                v-src="item.url"
                style="object-fit: contain; vertical-align: top;" />
              <div class="img-model">
                <i
                  class="el-icon-zoom-in set-img-zoom"
                  @click.stop="previewImage(primaryList, index)" />
                <i
                  class="el-icon-delete set-img-delete"
                  @click.stop="deleteImg('main', index)" />
              </div>
            </div>
            <div
              v-if="primaryList.length == 0"
              class="content-cross"
              @click="upLoadImg('main')">
              <input
                ref="primaryImgInput"
                accept="image/*"
                type="file"
                multiple
                class="file-input"
                @change="upLoad" />
              <el-button type="text" icon="el-icon-plus" class="cross" />
            </div>
          </flexbox>

          <!--（最多只能上传<span>{{ 9 - primaryList.length }}</span>张图片）-->
          <div class="detail-tip">
            {{ $t('crm.product.f80bde1bb772aa9524fa39a7e41ef8a0') }}
          </div>
        </div>
      </div>
    </div>
    <div class="detail-box">
      <div class="detail-item">
        <label class="detail-label">
          {{ $t('crm.product.b9bb26de3b125a072ae056c227dc060a') }}
        </label>
        <div class="detail">
          <flexbox class="detail-images" wrap="wrap">
            <div
              v-for="(item, index) in detailList"
              :key="index"
              class="show-img">
              <img
                :key="item.url"
                v-src="item.url"
                style="object-fit: contain; vertical-align: top;"
                class="cross-two" />
              <div class="img-model">
                <i
                  class="el-icon-zoom-in set-img-zoom"
                  @click.stop="previewImage(detailList, index)" />
                <i
                  class="el-icon-delete set-img-delete"
                  @click.stop="deleteImg('detail', index)" />
              </div>
            </div>
            <div
              v-if="detailList.length < 9"
              class="content-cross cross-two"
              @click="upLoadImg('detail')">
              <input
                ref="detailImgInput"
                accept="image/*"
                type="file"
                multiple
                class="file-input"
                @change="upLoad" />
              <el-button type="text" icon="el-icon-plus" class="cross" />
            </div>
          </flexbox>
          <div class="detail-tip">
            {{ $t('crm.product.bb66fb1d2ef2c231f1b93fe043f8ff67') }}
            <span>{{ 9 - detailList.length }}</span>
            {{ $t('crm.product.4d8526963f67d9c35eb9275b25b68b70') }}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/javascript">
import { crmFileSaveAPI, crmFileDeleteAPI } from '@/api/common'
import { filePathToBlob } from '@/utils'

export default {
  name: 'DetailImg',
  components: {},
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      primaryList: [],
      detailList: [],
      type: 'main',
      loading: false
    }
  },
  watch: {
    detail: {
      handler(data) {
        if (data) {
          this.primaryList = data.mainFile || []
          this.detailList = data.detailFile || []
        }
      },
      immediate: true
    }
  },
  mounted() {
  },
  methods: {
    upLoadImg(type) {
      this.type = type
      if (type === 'main') {
        this.$refs.primaryImgInput.click()
      } else {
        this.$refs.detailImgInput.click()
      }
    },

    /**
     * 处理选择的图片
     */
    selectImg(url) {
      filePathToBlob(url).then(blob => {
        var type = url.split('.').slice(-1) || 'png'
        var name = url.split('/').slice(-1)
        const file = new File([blob], name, { type: `image/${type}` })
        var even = {
          target: {
            files: [file]
          }
        }
        this.upLoad(even)
      })
    },

    /**
     * 上传
     */
    upLoad(event) {
      var files = Array.from(event.target.files)
      if (this.type === 'main' && files.length + this.primaryList.length > 9) {
        files = files.slice(0, 9 - this.primaryList.length)
        this.$message.error(this.$t('crm.product.d468c5946dcedf94fd1bf0f4d60970ff'))
      } else if (files.length + this.detailList.length > 9) {
        files = files.slice(0, 9 - this.detailList.length)
        this.$message.error(this.$t('crm.product.7ba7eda7899eae3004214fdd04a9ade0'))
      }

      for (let index = 0; index < files.length; index++) {
        const file = files[index]
        this.loading = true
        crmFileSaveAPI({ file: file }).then(res => {
          const data = res.data || {}
          if (this.type === 'main') {
            this.primaryList.push(data)
            this.loading = false
            this.$emit('change', 'mainFile', this.primaryList)
          } else {
            this.detailList.push(data)
            this.loading = false
            this.$emit('change', 'detailFile', this.detailList)
          }
        }).catch(() => {
          this.loading = false
        })
      }
    },

    /**
     * 删除图片
     */
    deleteImg(type, index) {
      const params = {}
      if (type == 'main') {
        params.id = this.primaryList[index].fileId
      } else {
        params.id = this.detailList[index].fileId
      }
      this.$confirm(
        this.$t('crm.product.75081323bf37ce437d95b5a9ca6ea2f9'),
        this.$t('crm.product.e9609668b8d5a5681de93c6c51298b99'),
        {
          confirmButtonText: this.$t('crm.product.b385352ab1d2ad5cdc2a340424bd4561'),
          cancelButtonText: this.$t('crm.product.1212ef04ce17cb578310b3efcce147de'),
          type: 'warning'
        }
      ).then(() => {
        this.loading = true
        crmFileDeleteAPI(params).then(res => {
          this.$message.success(this.$t('crm.product.f4a1ffb861fc81fbbd34e895288d2091'))
          if (type == 'main') {
            this.primaryList.splice(index, 1)
            this.$emit('change', 'mainFile', this.primaryList)
            this.$emit('delete', 'mainFile', this.primaryList)
          } else {
            this.detailList.splice(index, 1)
            this.$emit('change', 'detailFile', this.detailList)
            this.$emit('delete', 'detailFile', this.detailList)
          }
          this.loading = false
        }).catch(() => {
          this.loading = false
        })
      }).catch(() => {})
    },

    /**
     * 预览图片
     */
    previewImage(list, index) {
      this.$wkPreviewFile.preview({
        index: index,
        data: list
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.b-cont {
  position: relative;
}

.detail-box {
  .file-input {
    display: none;
  }

  .detail-item {
    margin-top: 8px;
  }

  .detail-label {
    margin-top: 20px;
    margin-left: 12px;
    font-weight: 500;
    word-break: keep-all;
    white-space: nowrap;
  }

  .detail-tip {
    margin-bottom: 20px;
    color: $--color-text-secondary;
  }

  // 详情
  .detail {
    padding: 0 12px;
  }

  // 图片详情
  .detail-images {
    margin: 20px 0;
    overflow-x: auto;
  }

  .show-img {
    position: relative;
    flex-shrink: 0;
    margin-right: 8px;
    margin-bottom: 8px;
    cursor: pointer;

    img {
      width: 100px;
      height: 76px;
      border-radius: $--border-radius-base;
    }

    .img-model {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      z-index: 10;
      line-height: 88px;
      visibility: hidden;
      background-color: #2d3037;
      border-radius: 6px;
      opacity: 0.8;

      .set-img-delete {
        font-size: 20px;
        color: white;
        cursor: pointer;
      }

      .set-img-zoom {
        padding-left: calc(50% - 30px);
        font-size: 20px;
        color: white;
        cursor: pointer;
      }
    }
  }

  .show-img:hover {
    .img-model {
      visibility: visible;
    }
  }

  .content-cross {
    position: relative;
    display: flex;
    flex-shrink: 0;
    width: 100px;
    height: 76px;
    margin-bottom: 5px;
    text-align: center;
    cursor: pointer;
    border: 1px #c0ccda dashed;
    border-radius: 6px;

    .cross {
      margin-left: calc(50% - 10px);
      font-size: 20px;
      color: #606266;
    }
  }

  .cross-two {
    height: 80px !important;
  }

  ::v-deep.el-icon-zoom-in {
    margin-right: 10px;
  }
}
</style>
